package com.rachel.studyapp.activity;

import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.gyf.barlibrary.ImmersionBar;
import com.rachel.studyapp.R;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by zhengshaorui on 2017/8/11.
 */

public abstract class BaseActivity extends AppCompatActivity {
    private static PermissionCallback mCallback;
    private static final int REQUESTCODE = 1;
    private RelativeLayout mContentLayout;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ActivityCollector.addActivity(this);
        ImmersionBar.with(this).init();
        setContentView(R.layout.activity_base);
        mContentLayout = (RelativeLayout) findViewById(R.id.content_layout);
        View view = LayoutInflater.from(this).inflate(getLayoutId(), mContentLayout, false);
        mContentLayout.addView(view);


    }



    @Override
    protected void onDestroy() {
        super.onDestroy();
        ActivityCollector.removeActivity(this);
        ImmersionBar.with(this).destroy();
    }

    public abstract int getLayoutId();



    //申请权限的方法
    public static void checkPermissions(String[] permissions, PermissionCallback callback) {
        mCallback = callback;
        Activity topActivity = ActivityCollector.getTopActivity();
        if (topActivity == null) {
            return;
        }
        List<String> permissionList = new ArrayList<>();
        for (String permission : permissions) {
            if (ContextCompat.checkSelfPermission(topActivity, permission) !=
                    PackageManager.PERMISSION_GRANTED) {
                permissionList.add(permission);
            }
        }
        if (!permissionList.isEmpty()) {
            ActivityCompat.requestPermissions((Activity) topActivity,
                    permissionList.toArray(new String[permissionList.size()]), REQUESTCODE);
        } else {
            mCallback.onGranted();
        }
    }

    //申请权限回调
    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        switch (requestCode) {
            case REQUESTCODE:
                if (grantResults.length > 0) {
                    List<String> deniedPermissions = new ArrayList<>();
                    for (int i = 0; i < grantResults.length; i++) {
                        if (grantResults[i] != PackageManager.PERMISSION_GRANTED) {
                            deniedPermissions.add(permissions[i]);
                            Toast.makeText(this, permissions[i] + "被拒绝了", Toast.LENGTH_SHORT).show();
                        }
                    }
                    if (!deniedPermissions.isEmpty()) {
                        mCallback.onDenied(deniedPermissions);
                    } else {
                        mCallback.onGranted();
                    }
                }
                break;
        }
    }


    @Override
    public void startActivity(Intent intent) {
        super.startActivity(intent);
        overridePendingTransition(R.anim.ac_start_in, R.anim.ac_start_out);
    }

    @Override
    public void finish() {
        super.finish();
        overridePendingTransition(R.anim.ac_finish_in, R.anim.ac_finish_out);
    }



}
